package dbs

import (
	"github.com/jinzhu/gorm"
	"time"
)

type DbDepartment struct {
	ID         uint      `json:"id" gorm:"primary_key"`                        //
	Department string    `json:"department" gorm:"type:varchar(32);not null"`  //科室
	Level      uint      `json:"level" gorm:"type:tinyint;not null;default:1"` //级别 1 患者  2职工
	CreatedAt  time.Time `json:"created_at" gorm:"type:datetime"`
	UpdatedAt  time.Time `json:"updated_at" gorm:"type:datetime"`
}

type DbDepartmentList struct {
	Department string //科室
}

type DepartmentList struct {
	Department string `json:"department"`
	Level      uint   `json:"level"`
}

func (d *DbDepartment) Query(req DepartmentList) (result []DbDepartment, err error) {
	err = D_Pool(func(db **gorm.DB) (err error) {
		conn := *db
		if req.Level > 0 {
			conn = conn.Where("level < ?", req.Level+1)
		}
		if req.Department != "" {
			conn = conn.Where("department like ?", "%"+req.Department+"%")
		}

		return conn.Model(&d).Find(&result).Error
	})
	return
}
